﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Printing;
using System.Drawing;
using System.Data;
using System.Data.OleDb;
using System.Windows;

namespace AllInOne
{
    class Printer : PrintDocument
    {
        public int LoginID;
        public const string SpaceLine = "-";
        public List<string> InfoList;

        /// <summary>
        /// 是否保存参数据到日志文件里面--调试时候使用
        /// </summary>
        public bool isFlag;

        public Printer()
        {
            float width = 2100;
            float height = 1300;//Properties.Settings.Default.PageSize.Height
            PrintDocument printDocument1 = this;
            MyPageSize ps = new MyPageSize();
            string defaultPrinterName = printDocument1.PrinterSettings.PrinterName;
            //MessageBox.Show("默认打印机名字："+ defaultPrinterName);

            //string myPageSizeName = "MyCustomePageSize";
            //默认纸张：新卷
            string myPageSizeName = "新卷";

            ps.SetPrintForm(defaultPrinterName, myPageSizeName, width, height);
            PaperSize ps1 = ps.GetPrintForm(defaultPrinterName, myPageSizeName);
            printDocument1.DefaultPageSettings.PaperSize = ps1;
            printDocument1.PrintController = new StandardPrintController();
            printDocument1.PrintPage += new PrintPageEventHandler(Printer_PrintPage);
        }

        void Printer_PrintPage(object sender, PrintPageEventArgs e)
        {
            try
            {
                PrintFormat1(InfoList,e);
                return;

            }
            catch (Exception ex)
            {
                MessageBox.Show("打印出错：" + ex.ToString());
            }
        }
        
        #region 格式一
        public void PrintFormat1(List<string> tempList,PrintPageEventArgs e)
        {
            //bool printPic = Properties.Settings.Default.启用车牌识别器 && Properties.Settings.Default.打印车头图;//打印车头图
            //bool printAxles = Properties.Settings.Default.打印轴信息 && Properties.Settings.Default.仪表型号 == (byte)(MyConst.Device.HengKe_JZ);//打印轴信息          
            ////A4纸8.27*11.69英寸，210mm*297mm
            ////另外的纸张大小101*101
            //if (printPic)
            //{
            //    PrintPic( e, printAxles);
            //}
            PrintPic(tempList,e, true);
        }

        public static void PrintPic(List<string> tempList, PrintPageEventArgs e, bool printSave)
        {
            try
            {
                //int fontsize = Properties.Settings.Default.PrintFontsize;
                if (tempList == null || tempList.Count != 5)
                {
                    LogHelper.WriteLog("打印出门证：传入参数不完整！");
                    return;
                }
                if (printSave)
                {
                    //LogHelper.WriteLog("打印出门证参数：传入参数不完整！");
                }
                int fontsize = 12;
                //A4纸8.27*11.69英寸，210mm*297mm
                int x = 0;
                int y = 0;
                int w = 0;
                int h = 0;
                int spH = 15;//行增加的边距
                int wC = 160;//列宽，每行5列
                //int xLeft = 5;//左边距     
                int xLeft = (int)(10 / 2.54);//左边距  
                int pageWidth = (int)(1010 / 2.54);//将毫米转化为英寸的百分之一
                int colWidth = (pageWidth - xLeft) / 6 - 5;//列宽，单位是英寸的百分之一
                wC = colWidth;
                StringFormat sf = new StringFormat
                {
                    LineAlignment = StringAlignment.Center,//上下居中对齐
                    Alignment = StringAlignment.Center//左右居中对齐
                };
                //出门证标题
                string s = tempList[0];
                Font font = new Font("宋体", 24);
                w = wC * 6;
                x = xLeft * 8;
                h = (int)e.Graphics.MeasureString(s, font).Height;
                Rectangle rect = new Rectangle(x, y, w, h);
                e.Graphics.DrawString(s, font, Brushes.Black, rect, sf);
                
                //第一行
                font = new Font("宋体", fontsize);
                s = "车牌号";
                h = (int)e.Graphics.MeasureString(s, font).Height + spH;
                y += h;
                w = wC;
                x = xLeft * 8;
                rect = new Rectangle(x, y, w*2, h);
                e.Graphics.DrawRectangle(Pens.Black, rect);//画线
                e.Graphics.DrawString(s, font, Brushes.Black, rect, sf);
                //内容
                s = tempList[1];
                x += w;
                rect = new Rectangle(x + wC, y, w*4, h);
                e.Graphics.DrawRectangle(Pens.Black, rect);
                e.Graphics.DrawString(s, font, Brushes.Black, rect, sf);

                //第二行
                s = "姓名";
                x = xLeft * 8;
                y += h;
                w = wC;
                rect = new Rectangle(x, y, w * 2, h);
                e.Graphics.DrawRectangle(Pens.Black, rect);
                e.Graphics.DrawString(s, font, Brushes.Black, rect, sf);

                s = tempList[2];
                x += w;
                rect = new Rectangle(x + wC, y, w*4, h);
                e.Graphics.DrawRectangle(Pens.Black, rect);
                e.Graphics.DrawString(s, font, Brushes.Black, rect, sf);

                //第三行
                s = "身份证号";
                x = xLeft * 8;
                y += h;
                w = wC;
                rect = new Rectangle(x, y, w * 2, h);
                e.Graphics.DrawRectangle(Pens.Black, rect);
                e.Graphics.DrawString(s, font, Brushes.Black, rect, sf);

                s = tempList[3];
                x += w;
                rect = new Rectangle(x + wC, y, w * 4, h);
                e.Graphics.DrawRectangle(Pens.Black, rect);
                e.Graphics.DrawString(s, font, Brushes.Black, rect, sf);

                //第四行
                s = "时间";
                x = xLeft * 8;
                y += h;
                w = wC;
                rect = new Rectangle(x, y, w * 2, h);
                e.Graphics.DrawRectangle(Pens.Black, rect);
                e.Graphics.DrawString(s, font, Brushes.Black, rect, sf);

                s = tempList[4];
                x += w;
                rect = new Rectangle(x + wC, y, w * 4, h);
                e.Graphics.DrawRectangle(Pens.Black, rect);
                e.Graphics.DrawString(s, font, Brushes.Black, rect, sf);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("打印出门证出错：" + ex.ToString());
                MessageBox.Show("失败：" + ex.ToString());
            }
        }
        #endregion
    }
}
